$(document).delegate('#nearby', 'pageshow', function () {
	
    var the_height = ($(window).height() - $(this).find('[data-role="header"]').height() - $(this).find('[data-role="footer"]').height() - $(this).find('[data-role="navbar"]').height());
    $(this).height($(window).height()).find('[data-role="content"]').height(the_height);
});

var map;
var marker;
var geocoder;
var infowindow;
$('#nearby').live('pageinit',function(){
	//Google Places

	//Google Maps
 	var mapOptions = {
    	zoom: 14,
    	mapTypeId: google.maps.MapTypeId.ROADMAP
  	}
  	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	// Try HTML5 geolocation
	if(navigator.geolocation) {
		//initialize geocoder
		geocoder = new google.maps.Geocoder();

	  navigator.geolocation.getCurrentPosition(function(position) {
		var pos = new google.maps.LatLng(position.coords.latitude,
										 position.coords.longitude);

		  marker = new google.maps.Marker({
			position: pos,
			map: map,
			draggable: true
		  });
		  var address = '';
		 /*
		  * function that converts lat lng to address 
		  */
		  function geocodePosition(pos) {
				geocoder.geocode({
					latLng: pos
				}, function(responses) {
					if (responses && responses.length > 0) {
						address = responses[0].formatted_address;
						infowindow.setContent(address);
						infowindow.open(map, marker);
					} else {
						console.log('Cannot determine address at this location.');
					}
				});
		  }		  
		
		  infowindow = new google.maps.InfoWindow({
			content: address
		  });
		  
		  google.maps.event.addListener(marker, 'click', function(e) {
			infowindow.open(map, marker);
		  });
		  
		  map.setCenter(pos);
		
		  // Add dragging event listeners.
		  google.maps.event.addListener(marker, 'dragstart', function() {
		  });
		  
		  google.maps.event.addListener(marker, 'drag', function() {
		  });
		  
		
		  google.maps.event.addListener(marker, 'dragend', function() {
			//updateMarkerStatus('Drag ended');
			geocodePosition(marker.getPosition());
		  });
		
	  }, function() {
		handleNoGeolocation(true);
	  });
	} else {
	  // Browser doesn't support Geolocation
	  handleNoGeolocation(false);
	}
	function handleNoGeolocation(errorFlag) {
		if (errorFlag) {
		  var content = 'Error: The Geolocation service failed.';
		} else {
		  var content = 'Error: Your browser doesn\'t support geolocation.';
		}
	
		var options = {
		  map: map,
		  position: new google.maps.LatLng(60, 105),
		  content: content
		};
	
		infowindow.setOptions(options);
		map.setCenter(options.position);
	}
	  
});//end $().live();


		


/*
function search() {
	//search function
	var addressField = $.trim(document.getElementById('search_address').value);
	if(addressField.length==0){
		return false;
	}
	geocoder = new google.maps.Geocoder();
    geocoder.geocode(
        {'address': addressField}, 
        function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK) { 
                var loc = results[0].geometry.location;
                // use loc.lat(), loc.lng()
				map.setCenter(loc);
				map.fitBounds(results[0].geometry.viewport);
				marker.setPosition(loc);
				infowindow.setContent(results[0].formatted_address);
				
            } 
            else {
                alert("Address not found: " + status); 
            } 
        }
    );
};
*/

function confirm(){
	var pos = marker.getPosition();
	var address = '';
	geocoder = new google.maps.Geocoder();
    
	  geocoder.geocode({
		latLng: pos
	  }, function(responses) {
		if (responses && responses.length > 0) {
		  address = responses[0].formatted_address;
		} else {
		  console.log('Cannot determine address at this location.');
		}
		});
	if(address.length==0){
		alert('Location not selected yet :(');
		return false;
	}
	//pass address to result page
	//...
	alert('pass this address to result page: '+address);	
	
	
	
	
}

function locate(){
	if(navigator.geolocation) {
		//initialize geocoder
		geocoder = new google.maps.Geocoder();
		navigator.geolocation.getCurrentPosition(function(position) {
			var pos = new google.maps.LatLng(position.coords.latitude,
										 position.coords.longitude);

		 	map.setCenter(pos);
			marker.setPosition(pos);			
			infowindow.close();
			
		}
		
		
		);
	}else{
		alert('Please turn on geolocation services');
	}
}
